/*     */ package com.zimbra.cs.mailclient.smtp;
/*     */ 
/*     */ import com.google.common.base.Preconditions;
/*     */ import com.google.common.base.Strings;
/*     */ import com.google.common.collect.Iterables;
/*     */ import com.sun.mail.smtp.SMTPMessage;
/*     */ import com.sun.mail.util.PropUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailclient.MailConfig.Security;
/*     */ import com.zimbra.cs.util.BuildInfo;
/*     */ import java.io.IOException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.Properties;
/*     */ import java.util.Set;
/*     */ import javax.mail.Address;
/*     */ import javax.mail.AuthenticationFailedException;
/*     */ import javax.mail.Message;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.Provider;
/*     */ import javax.mail.Provider.Type;
/*     */ import javax.mail.SendFailedException;
/*     */ import javax.mail.Session;
/*     */ import javax.mail.Transport;
/*     */ import javax.mail.URLName;
/*     */ import javax.mail.internet.InternetAddress;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.net.SocketFactory;
/*     */ import javax.net.ssl.SSLSocketFactory;
/*     */ import javax.security.auth.login.LoginException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SmtpTransport
/*     */   extends Transport
/*     */ {
/* 106 */   public static final Provider PROVIDER = new Provider(Provider.Type.TRANSPORT, "smtp", SmtpTransport.class.getName(), "Zimbra", BuildInfo.VERSION);
/*     */   
/*     */ 
/*     */   private SmtpConnection connection;
/*     */   
/*     */ 
/*     */   private final boolean ssl;
/*     */   
/*     */ 
/*     */   private final String protocol;
/*     */   
/*     */ 
/*     */   public SmtpTransport(Session session, URLName urlname)
/*     */   {
/* 120 */     this(session, urlname, false);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   SmtpTransport(Session session, URLName urlname, boolean ssl)
/*     */   {
/* 131 */     super(session, urlname);
/* 132 */     this.ssl = ssl;
/* 133 */     this.protocol = (ssl ? "smtps" : "smtp");
/*     */   }
/*     */   
/*     */   protected boolean protocolConnect(String host, int port, String user, String passwd)
/*     */     throws MessagingException
/*     */   {
/* 139 */     boolean auth = PropUtil.getBooleanSessionProperty(this.session, "mail." + this.protocol + ".auth", false);
/* 140 */     if ((auth) && ((user == null) || (passwd == null))) {
/* 141 */       return false;
/*     */     }
/*     */     
/* 144 */     if (port < 0) {
/* 145 */       port = PropUtil.getIntSessionProperty(this.session, "mail." + this.protocol + ".port", this.ssl ? 465 : 25);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 151 */     if (Strings.isNullOrEmpty(host)) {
/* 152 */       host = "localhost";
/*     */     }
/*     */     
/* 155 */     SmtpConfig config = new SmtpConfig();
/* 156 */     config.setHost(host);
/* 157 */     config.setPort(port);
/* 158 */     config.setDomain(this.session.getProperty("mail." + this.protocol + ".localhost"));
/* 159 */     config.setSecurity(PropUtil.getBooleanSessionProperty(this.session, "mail.smtp.starttls.enable", false) ? MailConfig.Security.TLS_IF_AVAILABLE : this.ssl ? MailConfig.Security.SSL : MailConfig.Security.NONE);
/*     */     
/* 161 */     config.setAllowPartialSend(PropUtil.getBooleanSessionProperty(this.session, "mail." + this.protocol + ".sendpartial", false));
/*     */     
/* 163 */     config.setConnectTimeout(PropUtil.getIntSessionProperty(this.session, "mail." + this.protocol + ".connectiontimeout", 0) / 1000);
/*     */     
/* 165 */     config.setReadTimeout(PropUtil.getIntSessionProperty(this.session, "mail." + this.protocol + ".timeout", 0) / 1000);
/*     */     
/* 167 */     config.setDsn(this.session.getProperty("mail." + this.protocol + ".dsn.notify"));
/*     */     
/* 169 */     Properties props = this.session.getProperties();
/* 170 */     Object socketFactory = props.get("mail." + this.protocol + ".socketFactory");
/* 171 */     if ((socketFactory instanceof SocketFactory)) {
/* 172 */       config.setSocketFactory((SocketFactory)socketFactory);
/*     */     }
/* 174 */     Object sslSocketFactory = props.get("mail." + this.protocol + ".ssl.socketFactory");
/* 175 */     if ((sslSocketFactory instanceof SSLSocketFactory)) {
/* 176 */       config.setSSLSocketFactory((SSLSocketFactory)sslSocketFactory);
/*     */     }
/*     */     
/* 179 */     if ((user != null) && (passwd != null)) {
/* 180 */       config.setAuthenticationId(user);
/*     */     }
/*     */     
/* 183 */     this.connection = new SmtpConnection(config);
/*     */     try {
/* 185 */       this.connection.connect();
/*     */     } catch (IOException e) {
/* 187 */       throw new MessagingException(e.getMessage(), e);
/*     */     }
/* 189 */     if ((auth) || ((user != null) && (passwd != null))) {
/*     */       try {
/* 191 */         this.connection.authenticate(passwd);
/*     */       } catch (LoginException e) {
/* 193 */         throw new AuthenticationFailedException(e.getMessage());
/*     */       } catch (IOException e) {
/* 195 */         throw new AuthenticationFailedException(e.getMessage());
/*     */       }
/*     */     }
/* 198 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void sendMessage(Message msg, Address[] rcpts)
/*     */     throws MessagingException
/*     */   {
/* 211 */     Preconditions.checkArgument(msg instanceof MimeMessage);
/* 212 */     Preconditions.checkState(this.connection != null);
/*     */     
/* 214 */     String sender = this.session.getProperty("mail." + this.protocol + ".from");
/* 215 */     if ((msg instanceof SMTPMessage)) {
/* 216 */       SMTPMessage smtpMsg = (SMTPMessage)msg;
/* 217 */       if (smtpMsg.getEnvelopeFrom() != null) {
/* 218 */         sender = smtpMsg.getEnvelopeFrom();
/*     */       }
/*     */     }
/*     */     try {
/* 222 */       if (sender != null) {
/* 223 */         this.connection.sendMessage(sender, rcpts, (MimeMessage)msg);
/*     */       } else {
/* 225 */         this.connection.sendMessage(rcpts, (MimeMessage)msg);
/*     */       }
/*     */     } catch (MessagingException e) {
/* 228 */       ZimbraLog.smtp.warn("Failed to send message", e);
/* 229 */       notify(e, msg, rcpts);
/*     */     } catch (IOException e) {
/* 231 */       ZimbraLog.smtp.warn("Failed to send message", e);
/* 232 */       notify(e, msg, rcpts);
/*     */     }
/* 234 */     notify(null, msg, rcpts);
/*     */   }
/*     */   
/*     */   public void close() throws MessagingException
/*     */   {
/* 239 */     if (this.connection != null) {
/* 240 */       this.connection.close();
/*     */     }
/* 242 */     if (isConnected()) {
/* 243 */       super.close();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void mail(String from)
/*     */     throws MessagingException
/*     */   {
/*     */     try
/*     */     {
/* 255 */       this.connection.mail(from);
/*     */     } catch (IOException e) {
/* 257 */       throw new MessagingException(e.getMessage(), e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void rset()
/*     */     throws MessagingException
/*     */   {
/*     */     try
/*     */     {
/* 268 */       this.connection.rset();
/*     */     } catch (IOException e) {
/* 270 */       throw new MessagingException(e.getMessage(), e);
/*     */     }
/*     */   }
/*     */   
/*     */   private void notify(Exception ex, Message msg, Address[] addrs) throws SendFailedException {
/* 275 */     Set<String> validRcpts = this.connection.getValidRecipients();
/* 276 */     Set<String> invalidRcpts = this.connection.getInvalidRecipients();
/*     */     
/* 278 */     List<Address> validAddrs = new ArrayList(validRcpts.size());
/* 279 */     List<Address> invalidAddrs = new ArrayList(invalidRcpts.size());
/* 280 */     for (Address addr : addrs) {
/* 281 */       if ((addr instanceof InternetAddress)) {
/* 282 */         InternetAddress iaddr = (InternetAddress)addr;
/* 283 */         if (validRcpts.contains(iaddr.getAddress())) {
/* 284 */           validAddrs.add(iaddr);
/* 285 */         } else if (invalidRcpts.contains(iaddr.getAddress())) {
/* 286 */           invalidAddrs.add(iaddr);
/*     */         }
/*     */       }
/*     */     }
/* 290 */     Address[] validSent = (Address[])Iterables.toArray(validAddrs, Address.class);
/* 291 */     Address[] validUnsent = new Address[0];
/* 292 */     Address[] invalid = (Address[])Iterables.toArray(invalidAddrs, Address.class);
/*     */     
/* 294 */     int notify = invalid.length > 0 ? 3 : ex != null ? 2 : 1;
/*     */     
/*     */ 
/* 297 */     notifyTransportListeners(notify, validSent, validUnsent, invalid, msg);
/* 298 */     switch (notify) {
/*     */     case 2: 
/* 300 */       throw new SendFailedException("MESSAGE_NOT_DELIVERED", ex, validSent, validUnsent, invalid);
/*     */     case 3: 
/* 302 */       throw new SendFailedException("MESSAGE_PARTIALLY_DELIVERED", ex, validSent, validUnsent, invalid);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailclient/smtp/SmtpTransport.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */